package cn.itcast.star.graph.core.utils;

import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.EasyExcel;

import java.util.ArrayList;
import java.util.List;

public class ETLData {
    /**
     * MaxKB数据库工具 > 文件转为excel
     * @param args
     */
    public static void main(String[] args) {
        //1.读文件
        List<String> lines = FileUtil.readUtf8Lines("D:\\StudyData\\Project02_AIGC应用与智能体开发\\02讲义资料\\Day01\\资料\\知识库\\Redis训练数据.md");

        //2.解析问题和答案
        String question = "";
        StringBuffer answer = new StringBuffer();
        List<MaxKBExcel> list = new ArrayList<>();
        MaxKBExcel maxKBExcel = new MaxKBExcel();
        for (String line : lines) {
            //判断是问题还是答案
            if (line.startsWith("##")) {
                System.out.println(question + answer);

                //3.2存入excel实体类和集合
                maxKBExcel = maxKBExcel.builder().title("redis")
                        .question(question)
                        .answer(answer.toString())
                        .build();
                list.add(maxKBExcel);

                answer.delete(0, answer.length());
                question = line;
            } else if (line != "") {
                //为回答
                answer.append(line);
            }
        }

        list.remove(0);

        //3.写入excel
        //3.1创建excel表头实体类
        //3.2写入excel
        EasyExcel.write("D:\\redis训练数据.xlsx", MaxKBExcel.class)
                .sheet("Redis面试题")
                .doWrite(list);
    }
}
